package Exercise;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise04 {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        HashSet set = new HashSet();
        person p1 = new person("AA", 1001);
        person p2 = new person("BB", 1002);
        set.add(p1);
        set.add(p2);
        p1.name = "CC";
        //删除不成功，因为删除时按照1001，CC的hashcode来删除，找不到原来AA 1001的hashcode的位置
        set.remove(p1);
        System.out.println(set);
        //能够添加成功，因为跟p1不在一个哈希表的位置上
        set.add(new person("CC",1001));
        System.out.println(set);
        //能够添加成功,放在p1的后边
        set.add(new person("AA",1001));
        System.out.println(set);

    }
}
class person{
    public String name;
    public int id;

    public person(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return name + "_" + id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        person person = (person) o;
        return id == person.id && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id);
    }
}